'use strict';

const {Controller} = require('egg');
const ResponseUtil = require('../utils/ResponseUtil');

class SysMenuController extends Controller {

    async getMenuList(){
        let query = this.ctx.query;
        const menus = await this.ctx.service.sysmenu.getMenuList(query);
        const resp = await this.ctx.service.sysmenu.buildMenuTree(menus);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async addMenu(){
        let body = this.ctx.request.body;
        const resp = await this.ctx.service.sysmenu.insertMenu(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async getMenuDetail(){
        let menuId = this.ctx.params.menuId;
        if(!menuId){
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysmenu.selectMenuById(menuId);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async updateMenu(){
        let body = this.ctx.request.body;
        if(!body.menuId){
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysmenu.updateMenu(body);
        return this.ctx.body = ResponseUtil.success(resp);
    }

    async deleteMenu(){
        let menuId = this.ctx.params.menuId;
        if(!menuId){
            return ResponseUtil.retArgErr();
        }
        const resp = await this.ctx.service.sysmenu.deleteMenuById(menuId);
        return this.ctx.body = ResponseUtil.success(resp);
    }

}

module.exports = SysMenuController;
